package com.java.springcloud.listener;

import lombok.extern.log4j.Log4j2;
import org.apache.rocketmq.spring.annotation.ConsumeMode;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Service;

import java.util.concurrent.ThreadLocalRandom;

/**
 * @author dushougudu
 */
@Log4j2
@Service
@RocketMQMessageListener(topic = "mongodb", selectorExpression = "*", consumerGroup = "test", consumeMode = ConsumeMode.ORDERLY)
public class RocketMqConsumerListener implements RocketMQListener<String> {
    ThreadLocalRandom random = ThreadLocalRandom.current();

    @Override
    public void onMessage(String message) {
        try {
            Thread.sleep(random.nextInt(1000, 5000));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if (message.equals("error")) {
            log.info("消息处理异常");
            throw new RuntimeException("消息处理异常");
        } else {
            log.info("收到 msg:{} 处理成功", message);
        }
    }
}
